﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsXuatChuyenKho
    {
        private string mDienGiai = "";
        private string mDuLieu = "NB";
        private int mDuyet;
        private string mKhoa = "";
        private string mKhoaDonVi = "";
        private string mKhoaDonViNhap = "";
        private string mKhoaDonViXuat = "";
        private string mKhoaKhoNhap = "";
        private string mKhoaKhoXuat = "";
        private string mLoaiTien = "";
        private string mNgayChungTu = "";
        private string mNguoiGiao = "";
        private string mNguoiNhan = "";
        private int mSend = 0;
        private string mSoChungTu = "";
        private double mSoTien = 0.0;
        private double mSoTienNT = 0.0;
        private double mTyGia = 0.0;

        public void ClearTemp(string pKeyTable)
        {
            string commandText = "DELETE FROM ST_XuatKhoChiTietTmp WHERE KhoaPhieuXuat = '" + pKeyTable.Trim() + "'";
            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
            commandText = "DELETE FROM ST_XuatKhoChiTietDichVuTmp WHERE KhoaPhieuXuat = '" + pKeyTable.Trim() + "'";
            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
        }

        public bool DeleteData(string strKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[4];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, strKhoa);
                commandParameters[1] = DBHelper.createParameter("@CheckAK", DbType.Double, ParameterDirection.Input, 1);
                commandParameters[2] = DBHelper.createParameter("@AmKho", DbType.Double, ParameterDirection.Output, 0);
                commandParameters[3] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                int num = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "ST_sp_XuatChuyenKhoDel", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[3].Value, 0, false))
                {
                    if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[2].Value, 0, false))
                    {
                        LVYModule.ShowWarning(modGeneral.Tranlated("Nghiệp vụ kh\x00f4ng thể thực hiện được v\x00ec \x00e2m kho!", ""));
                    }
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetDataPrint()
        {
            DataTable table;
            try
            {
                string commandText = "SELECT XCK.Khoa, XCK.SoChungTu, XCK.NgayChungTu, KX.Ten" + modGeneral.H_LANGUAGE + " as KhoXuat,XCK.NguoiNhan, KN.Ten" + modGeneral.H_LANGUAGE + " as KhoNhap, XCK.NguoiGiao, XCK.DienGiai  FROM ST_XuatChuyen XCK  LEFT JOIN DM_Kho KX ON KX.Khoa = XCK.KhoaDonViXuat  LEFT JOIN DM_Kho KN ON KN.Khoa = XCK.KhoaDonViNhap ORDER BY XCK.SoChungTu";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetDataPrintTongHop(string strCondition = "")
        {
            DataTable table;
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " WHERE " + strCondition;
                }
                string commandText = "SELECT XCK.Khoa, XCK.SoChungTu, dbo.Char2Date(XCK.NgayChungTu) as NgayChungTu, KX.Ten" + modGeneral.H_LANGUAGE + " as KhoXuat,XCK.NguoiNhan, KN.Ten" + modGeneral.H_LANGUAGE + " as KhoNhap, XCK.NguoiGiao, XCK.SoTien  FROM ST_XuatChuyen XCK  LEFT JOIN DM_Kho KX ON KX.Khoa = XCK.KhoaDonViXuat  LEFT JOIN DM_Kho KN ON KN.Khoa = XCK.KhoaDonViNhap" + str2 + " ORDER BY XCK.SoChungTu";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetDataReportDetails(string strKhoa)
        {
            DataTable table;
            try
            {
                string commandText = "  SELECT  XCK.SoChungTu, dbo.Char2Date(XCK.NgayChungTu) as NgayChungTu,HH.MA,  HH.Ten" + modGeneral.H_LANGUAGE + " as TenHangHoa,DVT.TenVIET as DVT, CT.QuyCach, CT.SoLuong ,CT.GiaVon   ,CT.TienVon,K.Ten" + modGeneral.H_LANGUAGE + " as KhoNhap,KK.Ten" + modGeneral.H_LANGUAGE + " as KhoXuat,XCK.NguoiGiao,XCK.NguoiNhan,XCK.DienGiai FROM ST_XuatChuyenChiTiet CT LEFT JOIN DM_DonViTinh DVT  ON DVT.Khoa = CT.KhoaDonViTinh  LEFT JOIN ST_XuatChuyen XCK  ON XCK.Khoa = CT.KhoaPhieuXuat LEFT JOIN DM_HangHoa HH  ON HH.Khoa = CT.KhoaHangHoa   LEFT JOIN  DM_Kho K on CT.KhoaKhoNhap = K.Khoa   LEFT JOIN DM_Kho KK on CT.KhoaKhoXuat =KK.Khoa   WHERE CT.KhoaPhieuXuat = '" + strKhoa + "'ORDER BY XCK.SoChungTu";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetHangHoaTonKho(string NamThang, string KhoaKho = "", string KhoaNhom = "")
        {
            DataTable table;
            string str3 = "";
            try
            {
                string str;
                str3 = " And TK.NamThang = '" + NamThang + "'";
                if (KhoaKho.Trim() != "")
                {
                    str3 = str3 + " And TK.KhoaKho = '" + KhoaKho + "'";
                }
                if (KhoaNhom.Trim() != "")
                {
                    str3 = str3 + " And HH.KhoaNhom = '" + KhoaNhom + "'";
                }
                switch (modGeneral.H_LOAIGIA)
                {
                    case "A":
                    case "F":
                        str = " ORDER BY TK.NgayNhap DESC, HH.Ma";
                        break;

                    case "L":
                        str = " ORDER BY TK.NgayNhap , HH.Ma";
                        break;
                }
                string commandText = "SELECT TK.KhoaKho, TK.KhoaHangHoa, HH.Ma, HH.Ten" + modGeneral.H_LANGUAGE + " as HangHoa, TK.SoLoHang, TK.NgayNhap, TK.SoSeri, DVT.Ten" + modGeneral.H_LANGUAGE + " as DonViTinh,  IsNull(TK.SoLuongTonDau,0) + IsNull(TK.SoLuongNhap,0) - IsNull(SoLuongXuat,0) as SoLuongTon, IsNull(TK.TriGiaTonDau,0) + IsNull(TK.TriGiaNhap, 0) - IsNull(TK.TriGiaXuat,0) as TriGiaTon, TK.DonGia  FROM ST_TonKhoDauKy TK left join DM_HangHoa HH on TK.KhoaHangHoa = HH.Khoa  LEFT JOIN DM_DonViTinh DVT on HH.KhoaDonViTinh = DVT.Khoa  Where IsNull(TK.SoLuongTonDau,0) + IsNull(TK.SoLuongNhap,0) - IsNull(SoLuongXuat,0) > 0  and IsNull(TK.TriGiaTonDau,0) + IsNull(TK.TriGiaNhap, 0) - IsNull(TK.TriGiaXuat,0) > 0 " + str3;
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetList(string strCondition = "")
        {
            DataTable table;
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " WHERE " + strCondition;
                }
                string commandText = "SELECT XK.Khoa, XK.SoChungTu, dbo.Char2Date(XK.NgayChungTu) as NgayChungTu, DT.Ten" + modGeneral.H_LANGUAGE + " as NhaCungCap,  XK.TienHangNT - XK.TienChietKhauNT + XK.TienThueVATNT as SoTienNT,  XK.TienHang - XK.TienChietKhau + XK.TienThueVAT as SoTien, XK.DienGiai,XK.GhiSo FROM ST_XuatKho XK LEFT JOIN DM_DoiTuong DT ON XK.KhoaDoiTuong = DT.Khoa " + str2;
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListDetails(string strKhoa)
        {
            DataTable table;
            try
            {
                string commandText = " SELECT CT.Khoa,CT.KhoaPhieuXuat,CT.KhoaKhoNhap, CT.KhoaKhoXuat, CT.KhoaHangHoa,  HH.MA, HH.Ten" + modGeneral.H_LANGUAGE + " as HangHoa,  CT.KhoaDonViTinh, DVT.Ten" + modGeneral.H_LANGUAGE + " as DonViTinh,  CT.QuyCach, CT.SoSeri, dbo.Char2Date(CT.HanSuDung) as HanSuDung, CT.NgayNhap, CT.SoLoHang as SoLo,  CT.SoLuong ,CT.GiaVon as DonGia, CT.TienVon as ThanhTien FROM ST_XuatChuyenChiTiet CT LEFT JOIN DM_DonViTinh DVT  ON DVT.Khoa = CT.KhoaDonViTinh LEFT JOIN DM_HangHoa HH  ON HH.Khoa = CT.KhoaHangHoa WHERE CT.KhoaPhieuXuat = '" + strKhoa + "'";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListXuatChuyenKho(string strCondition = "")
        {
            DataTable table;
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " WHERE " + strCondition;
                }
                string commandText = "SELECT XCK.Khoa, XCK.SoChungTu, dbo.Char2Date(XCK.NgayChungTu) as Ngay, KX.Ten" + modGeneral.H_LANGUAGE + " as KhoXuat,XCK.NguoiNhan, KN.Ten" + modGeneral.H_LANGUAGE + " as KhoNhap, XCK.NguoiGiao, XCK.DienGiai  FROM ST_XuatChuyen XCK  LEFT JOIN DM_Kho KX ON KX.Khoa = XCK.KhoaDonViXuat  LEFT JOIN DM_Kho KN ON KN.Khoa = XCK.KhoaDonViNhap" + str2 + " ORDER BY XCK.NgayChungTu, XCK.SoChungTu";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool IsDuplicateVoucherNo(string strVoucherNo, string strKeyTable)
        {
            string commandText = "SELECT * FROM ST_XuatChuyen Where Rtrim(SoChungTu) = '" + strVoucherNo.Trim() + "' AND Khoa <> '" + strKeyTable + "'";
            return (DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]) > 0);
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            try
            {
                string commandText = "SELECT * FROM ST_XuatChuyen WHERE Khoa = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mSoChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mNgayChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mLoaiTien = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mTyGia = Conversions.ToDouble(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetDecimal));
                    this.mNguoiGiao = Conversions.ToString(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetString));
                    this.mNguoiNhan = Conversions.ToString(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetString));
                    this.mKhoaKhoXuat = Conversions.ToString(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDonViXuat = Conversions.ToString(modGeneral.GetValue(dataReader, 8, modGeneral.typeOfColumn.GetString));
                    this.mKhoaKhoNhap = Conversions.ToString(modGeneral.GetValue(dataReader, 9, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDonViNhap = Conversions.ToString(modGeneral.GetValue(dataReader, 10, modGeneral.typeOfColumn.GetString));
                    this.mDienGiai = Conversions.ToString(modGeneral.GetValue(dataReader, 11, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDonVi = Conversions.ToString(modGeneral.GetValue(dataReader, 12, modGeneral.typeOfColumn.GetString));
                    this.mSend = Conversions.ToInteger(modGeneral.GetValue(dataReader, 13, modGeneral.typeOfColumn.GetInt32));
                    this.mSoTienNT = Conversions.ToDouble(modGeneral.GetValue(dataReader, 14, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoTien = Conversions.ToDouble(modGeneral.GetValue(dataReader, 15, modGeneral.typeOfColumn.GetDecimal));
                    this.mDuLieu = Conversions.ToString(modGeneral.GetValue(dataReader, 0x10, modGeneral.typeOfColumn.GetString));
                    this.mDuyet = Conversions.ToInteger(modGeneral.GetValue(dataReader, 0x11, modGeneral.typeOfColumn.GetInt32));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[20];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@SoChungTu", DbType.Int64, ParameterDirection.Input, this.mSoChungTu);
                commandParameters[2] = DBHelper.createParameter("@NgayChungTu", DbType.Int64, ParameterDirection.Input, this.mNgayChungTu);
                commandParameters[3] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, this.mLoaiTien);
                commandParameters[4] = DBHelper.createParameter("@TyGia", DbType.Date, ParameterDirection.Input, this.mTyGia);
                commandParameters[5] = DBHelper.createParameter("@NguoiGiao", DbType.Int64, ParameterDirection.Input, this.mNguoiGiao);
                commandParameters[6] = DBHelper.createParameter("@NguoiNhan", DbType.Int64, ParameterDirection.Input, this.mNguoiNhan);
                commandParameters[7] = DBHelper.createParameter("@KhoaKhoXuat", DbType.Int64, ParameterDirection.Input, this.mKhoaKhoXuat);
                commandParameters[8] = DBHelper.createParameter("@KhoaDonViXuat", DbType.Int64, ParameterDirection.Input, this.mKhoaDonViXuat);
                commandParameters[9] = DBHelper.createParameter("@KhoaKhoNhap", DbType.Int64, ParameterDirection.Input, this.mKhoaKhoNhap);
                commandParameters[10] = DBHelper.createParameter("@KhoaDonViNhap", DbType.Int64, ParameterDirection.Input, this.mKhoaDonViNhap);
                commandParameters[11] = DBHelper.createParameter("@DienGiai", DbType.Int64, ParameterDirection.Input, this.mDienGiai);
                commandParameters[12] = DBHelper.createParameter("@KhoaDonVi", DbType.Int64, ParameterDirection.Input, this.mKhoaDonVi);
                commandParameters[13] = DBHelper.createParameter("@Send", DbType.Double, ParameterDirection.Input, this.mSend);
                commandParameters[14] = DBHelper.createParameter("@SoTienNT", DbType.Date, ParameterDirection.Input, this.mSoTienNT);
                commandParameters[15] = DBHelper.createParameter("@SoTien", DbType.Date, ParameterDirection.Input, this.mSoTien);
                commandParameters[0x10] = DBHelper.createParameter("@DuLieu", DbType.Int64, ParameterDirection.Input, this.mDuLieu);
                commandParameters[0x11] = DBHelper.createParameter("@Duyet", DbType.Double, ParameterDirection.Input, this.mDuyet);
                commandParameters[0x12] = DBHelper.createParameter("@AmKho", DbType.Double, ParameterDirection.Output, 0);
                commandParameters[0x13] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                int num = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "ST_sp_XuatChuyenKho", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(commandParameters[0x13].Value, 0, false))
                {
                    return true;
                }
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[0x12].Value, 0, false))
                {
                    LVYModule.ShowWarning(modGeneral.Tranlated("Nghiệp vụ kh\x00f4ng thể thực hiện được v\x00ec \x00e2m kho!", ""));
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public string DienGiai
        {
            get
            {
                return this.mDienGiai;
            }
            set
            {
                this.mDienGiai = value;
            }
        }

        public string DuLieu
        {
            get
            {
                return this.mDuLieu;
            }
            set
            {
                this.mDuLieu = value;
            }
        }

        public int Duyet
        {
            get
            {
                return this.mDuyet;
            }
            set
            {
                this.mDuyet = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaDonVi
        {
            get
            {
                return this.mKhoaDonVi;
            }
            set
            {
                this.mKhoaDonVi = value;
            }
        }

        public string KhoaDonViNhap
        {
            get
            {
                return this.mKhoaDonViNhap;
            }
            set
            {
                this.mKhoaDonViNhap = value;
            }
        }

        public string KhoaDonViXuat
        {
            get
            {
                return this.mKhoaDonViXuat;
            }
            set
            {
                this.mKhoaDonViXuat = value;
            }
        }

        public string KhoaKhoNhap
        {
            get
            {
                return this.mKhoaKhoNhap;
            }
            set
            {
                this.mKhoaKhoNhap = value;
            }
        }

        public string KhoaKhoXuat
        {
            get
            {
                return this.mKhoaKhoXuat;
            }
            set
            {
                this.mKhoaKhoXuat = value;
            }
        }

        public string LoaiTien
        {
            get
            {
                return this.mLoaiTien;
            }
            set
            {
                this.mLoaiTien = value;
            }
        }

        public string NgayChungTu
        {
            get
            {
                return this.mNgayChungTu;
            }
            set
            {
                this.mNgayChungTu = value;
            }
        }

        public string NguoiGiao
        {
            get
            {
                return this.mNguoiGiao;
            }
            set
            {
                this.mNguoiGiao = value;
            }
        }

        public string NguoiNhan
        {
            get
            {
                return this.mNguoiNhan;
            }
            set
            {
                this.mNguoiNhan = value;
            }
        }

        public int Send
        {
            get
            {
                return this.mSend;
            }
            set
            {
                this.mSend = value;
            }
        }

        public string SoChungTu
        {
            get
            {
                return this.mSoChungTu;
            }
            set
            {
                this.mSoChungTu = value;
            }
        }

        public double SoTien
        {
            get
            {
                return this.mSoTien;
            }
            set
            {
                this.mSoTien = value;
            }
        }

        public double SoTienNT
        {
            get
            {
                return this.mSoTienNT;
            }
            set
            {
                this.mSoTienNT = value;
            }
        }

        public double TyGia
        {
            get
            {
                return this.mTyGia;
            }
            set
            {
                this.mTyGia = value;
            }
        }
    }
}

